package qlsl.androiddesign.view.subview.activityview;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.qlsl.androiddesign.kuaiwa.R;

import android.text.TextUtils;
import android.view.View;
import android.widget.BaseAdapter;
import android.widget.ListView;
import qlsl.androiddesign.activity.subactivity.OrderDetailActivity;
import qlsl.androiddesign.activity.subactivity.ShoppingOrderActivity;
import qlsl.androiddesign.adapter.subadapter.ShoppingOrderAdapter;
import qlsl.androiddesign.entity.commonentity.Pager;
import qlsl.androiddesign.entity.otherentity.ShoppingDetail;
import qlsl.androiddesign.http.service.subservice.OrderService;
import qlsl.androiddesign.library.pulltorefresh.PullToRefreshListView;
import qlsl.androiddesign.listener.OnPullRefreshListener;
import qlsl.androiddesign.util.runfeng.ToActivityUtil;
import qlsl.androiddesign.view.baseview.FunctionView;

/**
 * 订单页<br/>
 * 需要传入的键：<br/>
 * 传入的值类型： <br/>
 * 传入的值含义：<br/>
 * 是否必传 ：
 */
public class ShoppingOrderView extends FunctionView<ShoppingOrderActivity> {

	private PullToRefreshListView refreshView;

	private Pager pager;

	private List<ShoppingDetail> list = new ArrayList<ShoppingDetail>();

	public ShoppingOrderView(ShoppingOrderActivity activity) {
		super(activity);
		setContentView(R.layout.common_activity_refresh_list_view);
	}

	protected void initView(View view) {
		setTitle("我的订单");
		refreshView = findViewById(R.id.refreshListView);
	}

	protected void initData() {
		OrderService.queryOrderList(1, this, activity);
	}

	protected void initListener() {
		refreshView.setOnRefreshListener(refreshListener);
	}

	@SuppressWarnings("unchecked")
	public <T> void showData(T... t) {
		if (t[0] instanceof HashMap) {
			HashMap<String, Object> map = (HashMap<String, Object>) t[0];
			List<ShoppingDetail> list_result = (List<ShoppingDetail>) map.get("list");
			pager = (Pager) map.get("pager");
			if (pager.getPageNo() == 1) {
				resetList(list_result);
			} else {
				list.addAll(list_result);
			}
			notifyDataSetChanged();
		}
	}

	public <T> void showNoData(T... t) {

	}

	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.tv_contact:
			doClickPhoneNumberView(view);
			break;
		case R.id.list_item:
			doClickListItem(view);
			break;
		}
	}

	private void doClickPhoneNumberView(View view) {
		int position = refreshView.getRefreshableView().getPositionForView(view);
		ShoppingDetail item = (ShoppingDetail) refreshView.getAdapter().getItem(position);
		String mobile = item.getBusinessmobile();
		if (!TextUtils.isEmpty(mobile)) {
			ToActivityUtil.callPhoneActivity(activity, mobile);
		} else {
			showToast("卖家未提供电话号码");
		}
	}

	private void doClickListItem(View view) {
		int position = refreshView.getRefreshableView().getPositionForView(view);
		ShoppingDetail item = (ShoppingDetail) refreshView.getAdapter().getItem(position);
		startActivity(item, OrderDetailActivity.class);
	}

	private void resetList(List<ShoppingDetail> list_result) {
		list.clear();
		list.addAll(list_result);
	}

	private void notifyDataSetChanged() {
		BaseAdapter adapter = (BaseAdapter) refreshView.getBaseAdapter();
		if (adapter == null) {
			adapter = new ShoppingOrderAdapter(activity, list);
			refreshView.setAdapter(adapter);
		} else {
			adapter.notifyDataSetChanged();
		}
		refreshView.onRefreshComplete();
	}

	private OnPullRefreshListener<ListView> refreshListener = new OnPullRefreshListener<ListView>() {

		public void onPullDown() {
			OrderService.queryOrderList(1, ShoppingOrderView.this, activity);
		}

		public void onPullUp() {
			super.onPullUp(refreshView, pager);
		}

		public void nextPager() {
			OrderService.queryOrderList(pager.getPageNo() + 1, ShoppingOrderView.this, activity);
		}
	};

}
